# encoding: utf-8


"""

@author: tongzhenguo

@time: 2021/4/23 下午5:54

@desc:

保序回归 Isotonic Regression-Python
https://zhuanlan.zhihu.com/p/88623159

https://cf.meitu.com/confluence/pages/viewpage.action?pageId=193692143
"""
import numpy as np
import tensorflow as tf
from sklearn.isotonic import IsotonicRegression
from sklearn.utils import check_random_state

if __name__ == "__main__":
    # case 1:一维数组
    # x = tf.constant([9, 14, 10], dtype=tf.float32)
    # y, segments = tf.nn.isotonic_regression(x, decreasing=False, axis=0)
    # print(y)
    # print(segments)

    # case 2:
    n = 100
    x = np.arange(n)
    rs = check_random_state(0)
    y = rs.randint(-50, 50, size=(n,)) + 50. * np.log1p(np.arange(n))
    y, segments = tf.nn.isotonic_regression(y, decreasing=False, axis=0)
    print(y)
    print(segments)
    ir = IsotonicRegression()
    y_ = ir.fit_transform(x, y)
    print(y_)
